package action.general;

import bdd.ProduitBdd;
import service.securite.DroitsAcces;
import service.securite.Erreur;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import bean.produit.Produit;


public class ListeProduitServiceAction extends Action {
	private static final long serialVersionUID = 8690425543984355314L;

	protected void initSession(HttpServletRequest request )
	{
		HttpSession session = request.getSession();
	}	
	
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest pRequete,
			final HttpServletResponse pReponse) {
		
		ActionErrors errors = new ActionErrors();
		this.initSession(pRequete);

		int idService = Integer.parseInt(pRequete.getParameter("id_service"));		
		System.out.println("id service "+ idService);
		
		
		try {
			ArrayList<Produit> listP = ProduitBdd.getProduitsService(idService);
			System.out.println("Taille liste : "+listP.size());
			pRequete.setAttribute("produits", listP);
			return pMapping.findForward("success");
			
		} catch (Exception e) {
			e.printStackTrace();
			errors.add("errors",new ActionMessage("errors.exception"));
			saveErrors(pRequete,errors); 
			return (new ActionForward(pMapping.getInput()));
		}
	}
}